<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="topCardSpan">
        <el-card class="x-card">
          <div slot="header">
            <span class="card-header-title">筛选区</span>
            <div style="float: right;">
              <el-button style="padding-left: 10px" type="primary">查询</el-button>
              <el-button style="padding-left: 10px" type="primary">清空查询</el-button>
            </div>
          </div>
          <div>
            <el-form :inline="true" :model="page.search">
              <el-row :gutter="20">
                <el-col :span="filterFieldSpan">
                  <el-form-item label="名称">
                    <el-input v-model="page.search.name" placeholder="名称"></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="filterFieldSpan">
                  <el-form-item label="类型">
                    <el-select v-model="page.search.type" placeholder="类型">
                      <el-option label="类型一" value="1"></el-option>
                      <el-option label="类型二" value="2"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="filterFieldSpan">
                  <el-form-item label="日期范围">
                    <el-date-picker
                      v-model="page.search.dateRange"
                      type="daterange"
                      align="right"
                      placeholder="选择日期范围"
                      :picker-options="dateRangeOptions">
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="filterFieldSpan">
                  <el-form-item label="性别">
                    <el-radio-group v-model="page.search.sex">
                      <el-radio :label="0">保密</el-radio>
                      <el-radio :label="1">男</el-radio>
                      <el-radio :label="2">女</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
                <el-col :span="filterFieldSpan">
                  <el-form-item label="等级">
                    <el-checkbox-group v-model="page.search.levels">
                      <el-checkbox label="1">一级</el-checkbox>
                      <el-checkbox label="2">二级</el-checkbox>
                      <el-checkbox label="3">三级</el-checkbox>
                    </el-checkbox-group>
                  </el-form-item>
                </el-col>
              </el-row>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
        <el-card class="x-card">
          <div slot="header">
            <span class="card-header-title">列表信息区</span>
            <slot></slot>
          </div>
          <div>
            <el-table :data="page.list" border stripe style="width: 100%" v-loading.body="page.listLoading" @selection-change="handleSelectionChange">
              <el-table-column v-if="isSelect && selectMode === 'multi'" type="selection" width="55"></el-table-column>
              <el-table-column v-if="isSelect && selectMode === 'single'"  type="index" width="60">
                <template scope="scope">
                  <div @click="onSingleSelect(scope.row)">
                    <el-radio class="radio" v-model="singleSelectModel" :label="scope.row.id"></el-radio>
                  </div>
                </template>
              </el-table-column>
              <el-table-column v-else type="index" width="60"></el-table-column>
              <el-table-column prop="id" label="id" width="180"></el-table-column>
              <el-table-column prop="create" label="创建时间" width="180"></el-table-column>
              <el-table-column prop="modified" label="修改时间" width="180"></el-table-column>
              <el-table-column prop="dota2ItemId" label="dota2 id"></el-table-column>
              <el-table-column prop="enName" label="英文名称" width="180"></el-table-column>
              <el-table-column prop="cnName" label="中文名称" width="180"></el-table-column>
              <el-table-column prop="cost" label="价格"></el-table-column>
              <el-table-column prop="isSecretShop" label="神秘商店出售"></el-table-column>
              <el-table-column prop="isSideShop" label="边路商店出售"></el-table-column>
              <el-table-column prop="isNeedRecipe" label="需要卷轴"></el-table-column>
              <el-table-column v-if="!isSelect" label="操作" width="120">
                <template scope="scope">
                  <el-dropdown trigger="click">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-caret-bottom el-icon--right"></i>
                </span>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item command="edit">编辑</el-dropdown-item>
                      <el-dropdown-item command="delete">删除</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.currentPage"
                           :page-sizes="page.pageSizes"
                           :page-size="page.pageSize"
                           :layout="page.pageLayout"
                           :total="page.total">
            </el-pagination>
          </div>
        </el-card>
      </el-col>
      <el-col v-if="isSelect" :span="topCardSpan">
        <el-card class="x-card">
          <div>
            <el-table :data="selects" border stripe style="width: 100%">
              <el-table-column prop="date" label="日期" width="180"></el-table-column>
              <el-table-column prop="name" label="姓名" width="180"></el-table-column>
              <el-table-column prop="address" label="地址"></el-table-column>
              <el-table-column label="操作" width="120">
                <template scope="scope">
                  <el-button type="primary" icon="delete" size="mini" @click="delectSelect(scope.row)"></el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <div v-if="isSelect" class="form-operate">
      <el-button type="primary" @click="selectSave">保存</el-button>
      <el-button @click="selectCancel">取消</el-button>
    </div>
  </div>
</template>
<script>
  import {post, PAGESIZE, PAGESIZES, PAGELAYOUT} from '@/config'
  export default {
    props: {
      isSelect: {
        type: Boolean,
        default: false
      },
      selectMode: {
        type: String,
        default: 'multi'
      },
      selected: {
        type: Array,
        default () {
          return []
        }
      },
      selectSaveCallBack: {
        type: Function,
        default: undefined
      },
      selectCancelCallBack: {
        type: Function,
        default: undefined
      }
    },
    computed: {
      filterFieldSpan () {
        return this.isSelect ? 12 : 6
      },
      topCardSpan () {
        return this.isSelect ? 12 : 24
      }
    },
    mounted () {
      this.selects = this.selected
      if (this.selectMode === 'single' && this.selects.length > 0) {
        this.singleSelectModel = this.selects[0].id
      }
      this.getList()
    },
    data () {
      return {
        dateRangeOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick (picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
        },
        selects: [],
        singleSelectModel: '',
        page: {
          search: {
            name: '',
            type: '',
            dateRange: [],
            sex: '',
            levels: []
          },
          currentPage: 1,
          pageSize: PAGESIZE,
          pageSizes: PAGESIZES,
          pageLayout: PAGELAYOUT,
          total: 0,
          listLoading: false,
          list: []
        }
      }
    },
    methods: {
      getList () {
        let that = this
        let obj = {
          currentPage: this.page.currentPage,
          pageSize: this.page.pageSize
        }
        post('/api/item/list', obj, function (t) {
          if (t.status === 200) {
            that.page.list = t.data.data.list
            that.page.total = t.data.data.total
          } else {
            that.$message({
              message: t.errMsg,
              type: 'error'})
          }
        }, function (t) {
          that.$message({
            message: '网络异常',
            type: 'error'})
        })
      },
      handleSizeChange (val) {
        this.page.pageSize = val
        this.page.currentPage = 1
        this.getList()
      },
      handleCurrentChange (val) {
        this.page.currentPage = val
        this.getList()
      },
      handleSelectionChange (val) {
        this.selects = val
      },
      onSingleSelect (row) {
        this.selects = []
        this.selects.push(row)
      },
      selectSave () {
        if (this.selectSaveCallBack !== undefined) {
          this.selectSaveCallBack(this.selects)
        }
      },
      selectCancel () {
        if (this.selectCancelCallBack !== undefined) {
          this.selectCancelCallBack()
        }
      },
      delectSelect (row) {
        let index = -1
        this.selects.forEach(function (item, i) {
          if (item === row) {
            index = i
            return
          }
        })
        if (index !== -1) {
          this.selects.splice(index, 1)
          if (this.selectMode === 'single') {
            this.singleSelectModel = ''
          }
        }
      }
    }
  }
</script>
<style>
  .x-card {
    margin-top: 10px;
  }
  .card-header-title {
    line-height: 36px;
  }
  .el-dropdown-link {
    cursor: pointer;
    color: #20a0ff;
  }
  .el-icon-caret-bottom {
    font-size: 12px;
  }
  .el-icon--right {
    margin-left: 5px;
  }
  .form-operate {
    margin-top: 20px;
    text-align: center;
  }
</style>
